﻿using System;
using System.Collections.Generic;
using System.Linq;
using DMS.Business.Utility;
using DMS.Web.Common.Controls;
using DMS.Business.Modules.Organization;

namespace DMS.Web.Modules.SystemManagement.Organization
{
    public partial class DeptSettingForm : System.Web.UI.Page
    {
        private List<IConfigSetting> SettingForms
        {
            get
            {
                List<IConfigSetting> forms = new List<IConfigSetting>();

                forms.Add(this.csDeptIsCountUnit);
                forms.Add(this.csDeptRole);
                forms.Add(this.csFundsQuota);
                forms.Add(this.csMQuota);
                forms.Add(this.IsHault);
                forms.Add(this.IsTopDept);
                forms.Add(this.AssetDeptCode);
                return forms;
            }
        }

        #region 界面事件
        /// <summary>
        /// 界面加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.GetParams();
                this.InitForms();
            }
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            this.SaveForms();
        }
        #endregion

        #region 界面逻辑
        /// <summary>
        /// 获取参数
        /// </summary>
        private void GetParams()
        {
            Department dept = null;
            if (!string.IsNullOrEmpty(Request["DeptID"]))
            {
                dept = UserHelper.GetDepartmentByID(Request["DeptID"].ToString());
            }
            if (dept == null)
            {
                dept = UserHelper.GetRootDepartments().DefaultIfEmpty(null).FirstOrDefault();
            }
            if (dept != null)
            {
                this.hidContextDeptID.Value = dept.ID.ToString().ToUpper();
                this.lblContextDeptName.Text = dept.Name;
                this.lblContextDeptName.ToolTip = dept.FullName;
            }
        }
        /// <summary>
        /// 初始化表单
        /// </summary>
        private void InitForms()
        {
            List<IConfigSetting> forms = this.SettingForms;
            foreach (IConfigSetting form in forms)
            {
                form.MemberID = this.hidContextDeptID.Value;
                form.MemberType = ((int)ActorTypes.Dept).ToString();
                form.InitForm();
            }
        }
        /// <summary>
        /// 保存表单
        /// </summary>
        private void SaveForms()
        {
            int errorCounter = 0;
            string lastCode = string.Empty;
            List<IConfigSetting> forms = this.SettingForms;
            foreach (IConfigSetting form in forms)
            {
                try
                {
                    form.Save();
                }
                catch (Exception ex)
                {
                    errorCounter++;
                    lastCode = LogHelper.LogException(ex);
                }
            }

            if (errorCounter == 0)
            {
                ClientHelper.Instance.Alert("保存成功！");
            }
            else if (errorCounter == this.SettingForms.Count)
            {
                string msg = string.Format("保存失败，请与管理员联系。\\r\\n错误码：{0}", lastCode);
                ClientHelper.Instance.Alert(msg);
            }
            else
            {
                string msg = string.Format("部分参数保存失败，请与管理员联系。\\r\\n错误码：{0}", lastCode);
                ClientHelper.Instance.Alert(msg);
            }
        }
        #endregion
    }
}